﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Configuration;
using Artem;
using Artem.Web.UI.Controls;
using System.Data;
using Adex;
using MediaMonitor.Common;
using System.IO;

namespace MediaMonitor.Users
{
    public partial class OutDoorMap : System.Web.UI.UserControl
    {
        public Artem.Web.UI.Controls.GoogleMap GMap = new Artem.Web.UI.Controls.GoogleMap();

        public int MapWidth
        {
            set
            {
                GMap.Width = value;
            }
        }

        public int MapHeight
        {
            set
            {
                GMap.Height = value;
            }
        }

        private string GetKey()
        {
            return Session.SessionID;
        }

        private DataTable _dataSource;
        public DataTable DataSource
        {
            get { return _dataSource; }
            set { _dataSource = value; }
        }



        public void LoadMap(string Id, int Width, int Height, int zoom)
        {
            try
            {
                GMap.ID = Id;
                GMap.Key = ConfigurationManager.AppSettings["GoogleMapKey"];
                GMap.EnableContinuousZoom = false;
                GMap.EnableGoogleBar = false;
                GMap.ShowScaleControl = false;
                GMap.EnableMarkerManager = false;
                GMap.ShowMapTypeControl = false;
                GMap.Height = Height;
                GMap.Width = Width;
                GMap.Zoom = zoom;
                GMap.EnableGoogleBar = false;
                GMap.EnableGoogleMapState = false;
                GMap.ShowScaleControl = true;
                GMap.DefaultMapView = GoogleMapView.Hybrid;
                GMap.EnableScrollWheelZoom = true;
                GMap.EnableInfoWindow = true;
                GMap.EnableDragging = true;
                GMap.EnableContinuousZoom = true;

                this.Controls.Add(GMap);

                DataTable dtLocations;

                string key = GetKey();

                //if (Cache[key] != null)
                //    dtLocations = Cache[key] as DataTable;
                //else
                //{
                dtLocations = this.DataSource;

                Cache[key] = dtLocations;

                //}

                LoadDefaultMap(dtLocations);
                PanMap(0, 0);

            }
            catch
            {
            }
        }



        private void LoadDefaultMap(DataTable dt)
        {


            GMap.Markers.Clear();
            bool SetMapLang = false;



            foreach (DataRow dr in dt.Rows)
            {


                /*
             
    Double (required)	18.436389	    , -69.944444
    Degree	            18°26'11.5"N	,  69°56'40.2"W
    MM RF	            1826115		    ,  6956402
             
                 */

                string lat = dr["LatLong"].ToString().Split('/')[0];
                string lng = dr["LatLong"].ToString().Split('/')[1];

                if (lat == "" || lat.Length < 5 || lng == "" || lng.Length < 5)
                    continue;

                double Latitude = ConvertDegreeMinSecToDecimal(lat);   //Double.Parse(lat, new System.Globalization.CultureInfo("en-Us"));
                double Longitude = -ConvertDegreeMinSecToDecimal(lng);  //Double.Parse(lng, new System.Globalization.CultureInfo("en-Us"));
                //set the map starting location according to the latLong of first row of data
                if (!SetMapLang)
                {
                    GMap.ShowMapTypeControl = true;
                    GMap.Latitude = Latitude;
                    GMap.Longitude = Longitude;
                    SetMapLang = true;
                }

                string PhotoCode = dr["PhotoCode"].ToString().Trim();
                string ProductName = dr["ProductName"].ToString().Trim();
                string BrandName = dr["BrandName"].ToString().Trim();
                string AdvertiserName = dr["AdvertiserName"].ToString().Trim();
                string CarrierName = dr["CarrierName"].ToString().Trim();
                string VisibilityName = dr["VisibilityName"].ToString().Trim();
                string VerificationDate = dr["VerificationDate"].ToString().Trim();
                string RealCost = dr["RealCost"].ToString().Trim();
                string Location = dr["BetweenAvenue"].ToString().Trim();
                String ProductCode = String.Empty;
                ProductCode = dr["ProductCode"].ToString().Trim();


                GoogleMarker marker = new GoogleMarker(Latitude, Longitude);
                marker.Clickable = true;

                String ImageName = String.Empty;
                String ImagePath = String.Empty;
                ImageName = ProductCode + ".png";
                ImagePath = Server.MapPath("~") + "\\images\\maps\\" + ImageName;

                if (File.Exists(ImagePath))   //If Image Exists
                {
                    //marker.IconUrl = @"~\images\maps\" + ImageName;
                    marker.IconUrl = @"~\images\maps\"+ImageName+"?"+DateTime.Now.Ticks.ToString();
                }
                else
                {
                    //marker.IconUrl = @"~\images\maps\marker.png";
                    marker.IconUrl = @"~\images\maps\marker.png?" + DateTime.Now.Ticks.ToString();
                }

                //marker.IconUrl = @"~\images\maps\marker.png";
                //marker.IconSize = new GoogleSize(20, 25);
                marker.IconSize = new GoogleSize(35, 52);
                marker.IconAnchor = new GooglePoint(5, 20);
                marker.InfoWindowAnchor = new GooglePoint(5, 20);

                marker.OpenInfoBehaviour = OpenInfoBehaviour.Click;
                marker.InfoContent.Height = new Unit("500px");
                marker.Text = "<div style='font-size: small; font-family: arial'><table>";
                marker.Text += "<tr><td><b>Product:<b></td><td>" + ProductName + "</td></tr>";
                marker.Text += "<tr><td><b>Location:<b></td><td>" + Location + "</td></tr>";
                marker.Text += "<tr><td><b>Carrier:<b></td><td>" + CarrierName + "</td></tr>";
                marker.Text += "<tr><td><b>Visibility:<b></td><td>" + VisibilityName + "</td></tr>";
                marker.Text += "<tr><td><b>Verification:<b></td><td>" + VerificationDate + "</td></tr>";
                marker.Text += "<tr><td><b>Cost:<b></td><td>" + String.Format("${0}", RealCost) + "</td></tr>";
                marker.Text += "<tr><td colspan='2'><img src='../outdoor/" + PhotoCode + ".JPG' width='400px' /></td></tr>";
                marker.Text += "</table></div><br /><br /><br />";



                marker.Title = ProductName;
                marker.Show();
                //jsCountry += "arr[" + jsCounter + "] = \"" + dr["RegionCountry_ID"].ToString() + "|" + SetJSName(country) + "\";";

                GMap.Markers.Add(marker);

            }//end for 

            //this.Page.RegisterClientScriptBlock("countries", "<script>" + js  + "</script>");

            //GMap.MarkerEvents.OnClientClick = "MarkerClicked(this)";


        }


        public void PanMap(int x, int y)
        {
            GMap.PanDirection(x, y);

        }


        private double ConvertDegreeMinSecToDecimal(string DegMinSec)
        {


            int lat_deg = Utility.ToInt(DegMinSec.Substring(0, 2));
            int lat_min = Utility.ToInt(DegMinSec.Substring(2, 2));
            int lat_sec = Utility.ToInt(DegMinSec.Substring(4, 2));

            double lat_dec_min = (lat_min * 1.0 + (lat_sec / 60.0));
            double lat_dec_val = lat_deg * 1.0 + (lat_dec_min / 60.0);

            return lat_dec_val;

        }


    }

}